﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace Nokia_Portal_WPF
{
    /// <summary>
    /// Interaction logic for Apps.xaml
    /// </summary>
    public partial class Apps : Window
    {
        public Apps()
        {
            InitializeComponent();

            nag.Visibility = Visibility.Hidden;
            sag.Visibility = Visibility.Hidden;
            hag.Visibility = Visibility.Hidden;

            Nokia_cb.Visibility = Visibility.Hidden;
            Samsung_cb.Visibility = Visibility.Hidden;
            HTC_cb.Visibility = Visibility.Hidden;
        }

        private void LaunchFiddler2()
        {
            //Creates the script that Fiddler uses
            try
            {
                System.IO.Directory.CreateDirectory(System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + @"\Fiddler2");
                System.IO.Directory.CreateDirectory(System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + @"\Fiddler2\Scripts");
                File.Create(System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + @"\Fiddler2\Scripts\CustomRules.js").Close();
            }
            catch (Exception ex) { MessageBox.Show(ex.Message); }

            //Opens the sample script for editing
            StreamReader reader = new StreamReader(@".\SampleRules.js");
            string fileContent = reader.ReadToEnd();
            reader.Close();

            //Replaces the temp strings with user choices
            fileContent = fileContent.Replace("*;TEMPAPPS;*", cb1.SelectedItem.ToString().Replace("System.Windows.Controls.ComboBoxItem: ", ""));
            fileContent = fileContent.Replace("*;TEMPDEVICE;*", cb.SelectedItem.ToString().Replace("System.Windows.Controls.ComboBoxItem: ", ""));

            //Applies changes to the script
            StreamWriter writer = new StreamWriter(File.OpenWrite(System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + @"\Fiddler2\Scripts\CustomRules.js"));
            writer.Write(fileContent);
            writer.Close();

            //Starts Fiddler
            ProcessStartInfo start = new ProcessStartInfo();
            start.FileName = System.Environment.GetFolderPath(System.Environment.SpecialFolder.ProgramFiles) + @"\Fiddler2\Fiddler.exe";
            using (Process proc = Process.Start(start)) { }
        }

        private void cb1_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (cb1.SelectedItem.ToString().Replace("System.Windows.Controls.ComboBoxItem: ", "") == "Nokia")
            {
                Nokia_cb.Visibility = Visibility.Visible;
                Samsung_cb.Visibility = Visibility.Hidden;
                HTC_cb.Visibility = Visibility.Hidden;
            }
            if (cb1.SelectedItem.ToString().Replace("System.Windows.Controls.ComboBoxItem: ", "") == "SAMSUNG")
            {
                Samsung_cb.Visibility = Visibility.Visible;
                Nokia_cb.Visibility = Visibility.Hidden;
                HTC_cb.Visibility = Visibility.Hidden;
            }
            if (cb1.SelectedItem.ToString().Replace("System.Windows.Controls.ComboBoxItem: ", "") == "HTC")
            {
                HTC_cb.Visibility = Visibility.Visible;
                Nokia_cb.Visibility = Visibility.Hidden;
                Samsung_cb.Visibility = Visibility.Hidden;
            }
        }

        private void ToggleButton_Checked(object sender, RoutedEventArgs e)
        {
            Process[] pname = Process.GetProcessesByName("Fiddler");
            if (pname.Length == 0)
                MessageBox.Show("Please launch Fiddler2");
            else
            {
                nag.Visibility = Visibility.Visible;
                sag.Visibility = Visibility.Visible;
                hag.Visibility = Visibility.Visible;
            }
        }

        private void ToggleButton_Unchecked(object sender, RoutedEventArgs e)
        {
            Process[] pname = Process.GetProcessesByName("Fiddler");
            if (pname.Length == 0)
                MessageBox.Show("Please launch Fiddler2");
            else
            {
                nag.Visibility = Visibility.Hidden;
                sag.Visibility = Visibility.Hidden;
                hag.Visibility = Visibility.Hidden;
            }
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            LaunchFiddler2();
        }
    }
}
